package com.zhixianggou.caibaobox.timer;

import com.zhixianggou.caibaobox.commons.utils.DateUtil;
import com.zhixianggou.caibaobox.lottery.dataCollect.SportsLotteryCollectService;
import com.zhixianggou.caibaobox.lottery.dataCollect.WelfareLotteryCollectService;
import com.zhixianggou.caibaobox.lottery.dataCollect.utils.CookieUtil;
import com.zhixianggou.caibaobox.lottery.dataCollect.utils.UserAgentUtil;
import jakarta.annotation.Resource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author zyq
 * 8/22/24 11:50 AM
 */
@Component
public class ZeroClockQuartz {

    private Logger logger = LogManager.getLogger(ZeroClockQuartz.class);

    @Resource
    SportsLotteryCollectService sportsLotteryCollectService;

    @Resource
    WelfareLotteryCollectService welfareLotteryCollectService;

    /**
     * 每五分钟一次
     * 0 0/5 * * * ?
     */
    @Scheduled(cron = "0 06/50 * * * ?")
    public void zeroClock() throws InterruptedException {
        System.out.println("开始执行定时任务" + DateUtil.getTimeStamp());
        try{
            sportsLotteryCollectService.getData4DALETOU();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("定时任务--体彩大乐透数据录入 执行失败",e);
        }
        try{
            sportsLotteryCollectService.getData4ARRAY3();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("定时任务--体彩排列3数据录入 执行失败",e);
        }
        try{
            sportsLotteryCollectService.getData4ARRAY5();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("定时任务--体彩排列5数据录入 执行失败",e);
        }
        try{
            sportsLotteryCollectService.getData4SevenStar();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("定时任务--体彩七星彩数据录入 执行失败",e);
        }

        HttpHeaders headers = new HttpHeaders();
        String userAgent = UserAgentUtil.randomUserAgent();
        headers.set("cookie", CookieUtil.getCookie("https://www.cwl.gov.cn",userAgent));
        headers.set("user-agent", userAgent);

        try{
            welfareLotteryCollectService.getData4SSQ(headers);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("定时任务--福利彩票双色球数据录入 执行失败",e);
        }
        try{
            welfareLotteryCollectService.getData43d(headers);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("定时任务--福利彩票3D数据录入 执行失败",e);
        }
        try{
            welfareLotteryCollectService.getData4kl8(headers);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("定时任务--福利彩票快乐8数据录入 执行失败",e);
        }
        try{
            welfareLotteryCollectService.getData4QLC(headers);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("定时任务--福利彩票福彩QLC数据录入 执行失败",e);
        }

        System.out.println("结束执行定时任务" + DateUtil.getTimeStamp());

    }
}
